package top.dotgo.web.base.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Service;
import top.dotgo.kit.Const;
import top.dotgo.kit.ContextKit;
import top.dotgo.kit.bean.AccInfoBean;
import top.dotgo.kit.ex.DotMsg;
import top.dotgo.model.biz.dto.BizUserDTO;
import top.dotgo.model.biz.mapping.BizUserMapping;
import top.dotgo.model.pt.QPtOrgEntity;
import top.dotgo.model.pt.dto.PtOrgDTO;
import top.dotgo.model.pt.dto.PtStaffDTO;
import top.dotgo.model.pt.mapping.PtStaffMapping;
import top.dotgo.model.shop.QShopOrgEntity;
import top.dotgo.model.shop.dto.ShopOrgDTO;
import top.dotgo.model.shop.dto.ShopStaffDTO;
import top.dotgo.model.shop.mapping.ShopStaffMapping;
import top.dotgo.model.sys.dto.SysAccountDTO;
import top.dotgo.service.biz.IBizUserService;
import top.dotgo.service.pt.IPtOrgService;
import top.dotgo.service.pt.IPtStaffService;
import top.dotgo.service.shop.IShopOrgService;
import top.dotgo.service.shop.IShopStaffService;
import top.dotgo.service.sys.ISysAccountService;
import top.dotgo.web.base.R;

/**
 * 账号登录信息 等相关服务
 *
 * @author jornl
 * @date 2020/2/25 21:52 星期二
 */
@Service
public class AccInfoService {
    private final ISysAccountService sysAccountService;

    private final IPtStaffService ptStaffService;
    private final IPtOrgService ptOrgService;
    private final IShopStaffService shopStaffService;
    private final IShopOrgService shopOrgService;
    private final IBizUserService bizUserService;

    private final BizUserMapping userMapping;
    private final ShopStaffMapping shopStaffMapping;
    private final PtStaffMapping ptStaffMapping;

    public AccInfoService(ISysAccountService sysAccountService, IPtStaffService ptStaffService, IPtOrgService ptOrgService, IShopStaffService shopStaffService, IShopOrgService shopOrgService, IBizUserService bizUserService, BizUserMapping userMapping, ShopStaffMapping shopStaffMapping, PtStaffMapping ptStaffMapping) {
        this.sysAccountService = sysAccountService;
        this.ptStaffService = ptStaffService;
        this.ptOrgService = ptOrgService;
        this.shopStaffService = shopStaffService;
        this.shopOrgService = shopOrgService;
        this.bizUserService = bizUserService;
        this.userMapping = userMapping;
        this.shopStaffMapping = shopStaffMapping;
        this.ptStaffMapping = ptStaffMapping;
    }

    /**
     * 加载登陆信息
     *
     * @param accId accId
     * @param jti   jti
     * @return R
     */
    public R loadAccInfo(Long accId, String jti) {
        if (accId == null) {
            return R.err(DotMsg.LOGIN_ERR, DotMsg.LOGIN_ERR_MSG);
        }
        if (StrUtil.isEmpty(jti)) {
            return R.err(DotMsg.LOGIN_ERR, DotMsg.LOGIN_ERR_MSG);
        }

        //如果验证token 成功 开始加载用户数据
        SysAccountDTO account = sysAccountService.find(accId);
        if (account == null) {
            return R.err(DotMsg.PARAM_ERR, "加载用户信息丢失");
        }
        //根据账号类型加载数据
        if (account.getAccType().equals(Const.Type.acc.PT_ADMIN.v) || account.getAccType().equals(Const.Type.acc.PT_STAFF.v)) {
            loadByPt(account, jti);
        } else if (account.getAccType().equals(Const.Type.acc.SHOP_ADMIN.v) || account.getAccType().equals(Const.Type.acc.SHOP_STAFF.v)) {
            loadByShop(account, jti);
        } else {
            loadByUser(account, jti);
        }

        return R.ok();
    }

    /**
     * 加载普通用户 账号信息
     *
     * @param account account
     * @param jti     jti
     */
    private void loadByUser(SysAccountDTO account, String jti) {
        AccInfoBean infoBean = new AccInfoBean();
        BeanUtil.copyProperties(account, infoBean);
        infoBean.setJti(jti);
        //加载staff
        BizUserDTO user = bizUserService.findFirst(new BizUserDTO().setAccId(account.getAccId()));
        infoBean.setUser(userMapping.vo(user));
        infoBean.setUserId(user.getId());

        ContextKit.setAccInfo(infoBean);
    }

    /**
     * 加载 供应商 账号信息
     *
     * @param account account
     * @param jti     jti
     */
    private void loadByShop(SysAccountDTO account, String jti) {
        AccInfoBean infoBean = new AccInfoBean();
        BeanUtil.copyProperties(account, infoBean);
        infoBean.setJti(jti);
        //加载staff
        ShopStaffDTO staff = shopStaffService.findFirst(new ShopStaffDTO().setAccId(account.getAccId()));
        infoBean.setShopStaff(shopStaffMapping.vo(staff));
        infoBean.setUserId(staff.getId());

        //topCompany
        infoBean.setTopCompanyId(staff.getTopCompanyId());
        ShopOrgDTO topCompany = shopOrgService.find(staff.getTopCompanyId(), QShopOrgEntity.shopOrgEntity.orgName);
        if (topCompany != null) {
            infoBean.setTopCompanyName(topCompany.getOrgName());
        }
        //company
        infoBean.setCompanyId(staff.getCompanyId());
        ShopOrgDTO company = shopOrgService.find(staff.getCompanyId(), QShopOrgEntity.shopOrgEntity.orgName);
        if (company != null) {
            infoBean.setCompanyName(company.getOrgName());
        }
        //department
        infoBean.setDepId(staff.getDepId());
        ShopOrgDTO department = shopOrgService.find(staff.getDepId(), QShopOrgEntity.shopOrgEntity.orgName);
        if (department != null) {
            infoBean.setDepName(department.getOrgName());
        }

        ContextKit.setAccInfo(infoBean);
    }

    /**
     * 加载 平台 用户信息
     *
     * @param account account
     * @param jti     jti
     */
    private void loadByPt(SysAccountDTO account, String jti) {
        AccInfoBean infoBean = new AccInfoBean();
        BeanUtil.copyProperties(account, infoBean);
        infoBean.setJti(jti);
        //加载staff
        PtStaffDTO staff = ptStaffService.findFirst(new PtStaffDTO().setAccId(account.getAccId()));
        infoBean.setPtStaff(ptStaffMapping.vo(staff));
        infoBean.setUserId(staff.getId());

        //topCompany
        infoBean.setTopCompanyId(staff.getTopCompanyId());
        PtOrgDTO topCompany = ptOrgService.find(staff.getTopCompanyId(), QPtOrgEntity.ptOrgEntity.orgName);
        if (topCompany != null) {
            infoBean.setTopCompanyName(topCompany.getOrgName());
        }
        //company
        infoBean.setCompanyId(staff.getCompanyId());
        PtOrgDTO company = ptOrgService.find(staff.getCompanyId(), QPtOrgEntity.ptOrgEntity.orgName);
        if (company != null) {
            infoBean.setCompanyName(company.getOrgName());
        }
        //department
        infoBean.setDepId(staff.getDepId());
        PtOrgDTO department = ptOrgService.find(staff.getDepId(), QPtOrgEntity.ptOrgEntity.orgName);
        if (department != null) {
            infoBean.setCompanyName(department.getOrgName());
        }

        ContextKit.setAccInfo(infoBean);
    }

}
